package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.enums.YN;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.BatchPlanMapper;
import com.wpen.unit.infra.mybatis.job.po.BatchPlanPo;
import com.wpen.unit.infra.mybatis.job.po.BatchPlanPo.BatchPlanColumns;
import com.wpen.unit.infra.repository.job.BatchPlanRepository;
import com.wpen.unit.utils.StringUtil;

/**
 * 调度排程维护.
 * 
 * @author pg
 *
 */
@Repository
@Transactional
public class BatchPlanRepositoryImpl extends ServiceImpl<BatchPlanMapper, BatchPlanPo> implements BatchPlanRepository {

    @Override
    public long count(BatchPlanPo schedulePlanPo) {
        QueryWrapper<BatchPlanPo> queryWrapper = createQuery(schedulePlanPo);
        return super.count(queryWrapper);
    }

    @Override
    public List<BatchPlanPo> queryList(int index, int size, BatchPlanPo schedulePlanPo) {
        QueryWrapper<BatchPlanPo> queryWrapper = createQuery(schedulePlanPo);
        if (size > 0) {
            Page<BatchPlanPo> page = new Page<>(index, size, false);
            return super.page(page, queryWrapper).getRecords();
        }
        // queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<BatchPlanPo> queryListByNextTime(int size, long nextTime) {
        QueryWrapper<BatchPlanPo> queryWrapper = Wrappers.query();
        queryWrapper.lt(BatchPlanColumns.TRIGGER_NEXT_TIME.name(), nextTime);
        queryWrapper.eq(BatchPlanColumns.ENABLED.name(), YN.Yes.value());
        Page<BatchPlanPo> page = new Page<>(0, size, false);
        return super.page(page, queryWrapper).getRecords();
    }

    private QueryWrapper<BatchPlanPo> createQuery(BatchPlanPo schedulePlanPo) {
        QueryWrapper<BatchPlanPo> queryWrapper = Wrappers.query();
        if (schedulePlanPo.getId() != null) {
            queryWrapper.eq(BatchPlanColumns.ID.name(), schedulePlanPo.getId());
        }
        if (StringUtil.isNotEmpty(schedulePlanPo.getBizCode())) {
            queryWrapper.eq(BatchPlanColumns.BIZ_CODE.name(), schedulePlanPo.getBizCode());
        }
        if (schedulePlanPo.getTriggerLastTime() != null) {
            queryWrapper.eq(BatchPlanColumns.TRIGGER_LAST_TIME.name(), schedulePlanPo.getTriggerLastTime());
        }
        if (schedulePlanPo.getTriggerNextTime() != null) {
            queryWrapper.eq(BatchPlanColumns.TRIGGER_NEXT_TIME.name(), schedulePlanPo.getTriggerNextTime());
        }
        if (StringUtil.isNotEmpty(schedulePlanPo.getEnabled())) {
            queryWrapper.eq(BatchPlanColumns.ENABLED.name(), schedulePlanPo.getEnabled());
        }
        return queryWrapper;
    }

    @Override
    public BatchPlanPo queryFirst(BatchPlanPo schedulePlanPo) {
        QueryWrapper<BatchPlanPo> queryWrapper = Wrappers.query();
        if (schedulePlanPo.getId() != null) {
            queryWrapper.eq(BatchPlanColumns.ID.name(), schedulePlanPo.getId());
        }
        if (StringUtil.isNotEmpty(schedulePlanPo.getBizCode())) {
            queryWrapper.eq(BatchPlanColumns.BIZ_CODE.name(), schedulePlanPo.getBizCode());
        }
        if (schedulePlanPo.getTriggerNextTime() != null) {
            queryWrapper.eq(BatchPlanColumns.TRIGGER_NEXT_TIME.name(), schedulePlanPo.getTriggerNextTime());
        }
        if (StringUtil.isNotEmpty(schedulePlanPo.getEnabled())) {
            queryWrapper.eq(BatchPlanColumns.ENABLED.name(), schedulePlanPo.getEnabled());
        }
        // queryWrapper.orderBy(true, false, SchedulePlanPoColumns.ID.name());
        return super.getOne(queryWrapper);
    }

    @Override
    public BatchPlanPo queryById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean insert(BatchPlanPo schedulePlanPo) {
//        schedulePlanPo.setVersion(1);
//        schedulePlanPo.setTimeStamp(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(schedulePlanPo);
    }

    @Override
    public boolean insertBatch(List<BatchPlanPo> listBatchPlanPo) {
        int rs = getBaseMapper().insertBatchSomeColumn(listBatchPlanPo);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(BatchPlanPo schedulePlanPo) {
        UpdateWrapper<BatchPlanPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(BatchPlanColumns.ID.name(), schedulePlanPo.getId());
        schedulePlanPo.setBizCode(null);
        schedulePlanPo.setBatchCode(null);
        boolean rs = super.update(schedulePlanPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<BatchPlanPo> updateWrapper = Wrappers.update();
        updateWrapper.in(BatchPlanColumns.ID.name(), (Object[]) ids);
        return super.remove(updateWrapper);
    }

//    public BatchPlanPo queryByExample(BatchPlanPo schedulePlanPo) {
//        QueryWrapper<BatchPlanPo> queryWrapper = Wrappers.query();
//        if (schedulePlanPo.getId() != null) {
//            queryWrapper.eq(BatchPlanColumns.ID.name(), schedulePlanPo.getId());
//        }
//        if (StringUtil.isNotEmpty(schedulePlanPo.getBizCode())) {
//            queryWrapper.eq(BatchPlanColumns.BIZ_CODE.name(), schedulePlanPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(schedulePlanPo.getScheduleType())) {
//            queryWrapper.eq(BatchPlanColumns.SCHEDULE_TYPE.name(), schedulePlanPo.getScheduleType());
//        }
//        if (StringUtil.isNotEmpty(schedulePlanPo.getScheduleConf())) {
//            queryWrapper.eq(BatchPlanColumns.SCHEDULE_CONF.name(), schedulePlanPo.getScheduleConf());
//        }
//        if (schedulePlanPo.getTriggerLastTime() != null) {
//            queryWrapper.eq(BatchPlanColumns.TRIGGER_LAST_TIME.name(), schedulePlanPo.getTriggerLastTime());
//        }
//        if (schedulePlanPo.getTriggerNextTime() != null) {
//            queryWrapper.eq(BatchPlanColumns.TRIGGER_NEXT_TIME.name(), schedulePlanPo.getTriggerNextTime());
//        }
//        if (StringUtil.isNotEmpty(schedulePlanPo.getEnabled())) {
//            queryWrapper.eq(BatchPlanColumns.ENABLED.name(), schedulePlanPo.getEnabled());
//        }
//		Page<BatchPlanPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, SchedulePlanPoColumns.ID.name());
//    }

}
